package com.umeox.watch.moto.launcher.ui;

import static com.umeox.watch.moto.launcher.adapter.ChallengeAdapter.TYPE_CYCLE;
import static com.umeox.watch.moto.launcher.adapter.ChallengeAdapter.TYPE_FOOTBALL;
import static com.umeox.watch.moto.launcher.adapter.ChallengeAdapter.TYPE_GYM;
import static com.umeox.watch.moto.launcher.adapter.ChallengeAdapter.TYPE_JUMPROPE;
import static com.umeox.watch.moto.launcher.adapter.ChallengeAdapter.TYPE_RUN;
import static com.umeox.watch.moto.launcher.adapter.ChallengeAdapter.TYPE_WALK;
import static com.umeox.watch.moto.launcher.adapter.ChallengeAdapter.TYPE_YOGA;
import static com.umeox.watch.moto.launcher.fragment.PedometerFragment.EXTRA_KEY_AUTO;
import static com.umeox.watch.moto.launcher.fragment.PedometerFragment.EXTRA_KEY_ID;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.Vibrator;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.umeox.watch.moto.dataservice.DataProvider;
import com.umeox.watch.moto.launcher.R;
import com.umeox.watch.moto.pedometer.base.BaseApp;
import com.umeox.watch.moto.pedometer.base.TodayChallenge;

import java.util.Locale;

public class ChallengeTriggerActivity extends ThemeAppCompatActivity implements View.OnClickListener {
    public static final String EXTRA_CHALLENGE_ID = "id";
    private long[] pattern = {600, 600, 600, 600};
    private TextView mMsgView;
    private TextView mThirdTv;
    private ImageView mIconView;
    private Vibrator mVibrator;
    private long mChallengeId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setContentView(R.layout.notification_dialog_challenge);
        if (mVibrator == null) {
            mVibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        }
        mVibrator.vibrate(pattern, 0);
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "My:Tag");
        wakeLock.acquire(5000);

        mMsgView = findViewById(R.id.tvMsg);
        mIconView = findViewById(R.id.iv_type);
        mThirdTv = findViewById(R.id.tv_dialog_third);

        mChallengeId = getIntent().getLongExtra(EXTRA_CHALLENGE_ID, 0);
        TodayChallenge todayChallenge = getTodayChallenge(mChallengeId);

        bind(todayChallenge);

        findViewById(R.id.rl_root).setOnClickListener(this);
    }

    private TodayChallenge getTodayChallenge(long id) {

        Cursor _cursor = BaseApp.getAppContext().getContentResolver().query(DataProvider.SportsChallengeProvider.getUri(), null, "id = ?", new String[]{String.valueOf(id)}, null);
        if (_cursor == null) return null;
        TodayChallenge _item = null;
        try {
            final int _cursorIndexOfId = _cursor.getColumnIndex("id");
            final int _cursorIndexOfSportType = _cursor.getColumnIndex("sportType");
            final int _cursorIndexOfDuration = _cursor.getColumnIndex("duration");
            final int _cursorIndexOfReminderTime = _cursor.getColumnIndex("reminderTime");
            final int _cursorIndexOfRewardValue = _cursor.getColumnIndex("rewardValue");
            final int _cursorIndexOfIsReminder = _cursor.getColumnIndex("isReminder");

            while (_cursor.moveToNext()) {

                _item = new TodayChallenge();
                _item.id = _cursor.getLong(_cursorIndexOfId);
                _item.sportType = _cursor.getInt(_cursorIndexOfSportType);
                _item.duration = _cursor.getInt(_cursorIndexOfDuration);
                if (_cursor.isNull(_cursorIndexOfReminderTime)) {
                    _item.reminderTime = null;
                } else {
                    _item.reminderTime = _cursor.getString(_cursorIndexOfReminderTime);
                }
                _item.rewardValue = _cursor.getInt(_cursorIndexOfRewardValue);
                _item.isReminder = _cursor.getInt(_cursorIndexOfIsReminder);
            }

        } finally {

            _cursor.close();

        }
        return _item;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_root:
                Intent intent = new Intent();
                intent.setClassName("com.umeox.moto.watch.fitness", "com.umeox.moto.watch.fitness.ui.ChallengeTipActivity");
                Bundle mBundle = new Bundle();
                mBundle.putLong(EXTRA_KEY_ID, mChallengeId);
                mBundle.putBoolean(EXTRA_KEY_AUTO, false);
                intent.putExtras(mBundle);
                startActivity(intent);

                break;
            default:
                break;
        }
        finish();
    }

    public void bind(TodayChallenge challengeBean) {
        if(challengeBean == null){
            return;
        }
        int resId = R.drawable.ic_walk_dialog;
        String challengeDescStr = mContext.getResources().getString(R.string.walk_for_str);

        switch (challengeBean.sportType) {
            case TYPE_WALK:
                resId = R.drawable.ic_walk_dialog;
                challengeDescStr = mContext.getResources().getString(R.string.walk_for_str);
                break;
            case TYPE_RUN:
                resId = R.drawable.ic_run_dialog;
                challengeDescStr = mContext.getResources().getString(R.string.run_for_str);
                break;

            case TYPE_CYCLE:
                resId = R.drawable.ic_ride_dialog;
                challengeDescStr = mContext.getResources().getString(R.string.cycle_for_str);
                break;
            case TYPE_FOOTBALL:
                resId = R.drawable.ic_football_dialog;
                challengeDescStr = mContext.getResources().getString(R.string.football_for_str);
                break;
            case TYPE_GYM:
                resId = R.drawable.ic_gym_dialog;
                challengeDescStr = mContext.getResources().getString(R.string.gym_for_str);
                break;
            case TYPE_JUMPROPE:
                resId = R.drawable.ic_jump_dialog;
                challengeDescStr = mContext.getResources().getString(R.string.jump_rope_for_str);
                break;
            case TYPE_YOGA:
                resId = R.drawable.ic_yoga_dialog;
                challengeDescStr = mContext.getResources().getString(R.string.yoga_rope_for_str);
                break;
        }

        mIconView.setImageResource(resId);
        mMsgView.setText(String.format(Locale.getDefault(), challengeDescStr, challengeBean.duration));
        mThirdTv.setText(String.format(Locale.getDefault(), "+ %d", challengeBean.rewardValue));

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mVibrator != null) {
            mVibrator.cancel();
        }
    }
}
